home *** CD-ROM | disk | FTP | other *** search
- #include <A4Stuff.h>
- #include "FlyPaperPatch.h"
- #include "FlyPaperINIT.h"
- #include "FlyPaperDragUtils.h"
-
- typedef enum {
- kIdleState,
- kStartedPegged,
- kDraggingState,
- kPeggedState,
- kDroppedState
- } MachineState;
-
- typedef enum {
- kLeftSide,
- kRightSide
- } Side;
-
- typedef enum {
- kUnknown,
- kAcceptable,
- kUnacceptable
- } Acceptability;
-
- #define kFlyAnimateDelay 2
-
- MachineState gMachineState = kIdleState;
- RgnHandle gNarrowGrayRgn = NULL;
- Cursor gOldCursor;
-
- short gWhichFly = 0;
- unsigned long gFlyTime = 0;
- FlyPaperGestaltPtr gFlyPaperINITData = NULL;
- Rect gGrayRgnRect;
- Acceptability gAcceptability;
-
- static
- Acceptability CheckAcceptability (DragReference dragRef)
- {
- if (gAcceptability == kUnknown)
- gAcceptability = AcceptableDrag (dragRef) ? kAcceptable : kUnacceptable;
-
- return gAcceptability;
- }
-
- static FlyPaperGestaltPtr GetINITData ()
- {
- if (gFlyPaperINITData == NULL) {
- Gestalt (kSignature, (long*) &gFlyPaperINITData);
- }
- return gFlyPaperINITData;
- }
-
- static void RememberCursor ()
- {
- BlockMoveData ((void*) 0x00000844, &gOldCursor, sizeof (Cursor));
- }
-
- static void MySetCursor (Cursor* cursor)
- {
- // Call SetCursor directly through jSetCursor, since someone's stupid patch does not allow
- // SetCursor to operate properly. :-(
- // Wish I knew why I was passing in 16, but the ROM does it. When in ROMe...
-
- typedef pascal void (*jSetCrsrProc) (Point, short, void*, void*);
- (*((jSetCrsrProc*) 0x00000818)) (cursor -> hotSpot, 16, &cursor -> data, &cursor -> mask);
- }
-
- static void AnimateFly ()
- {
- unsigned long now = TickCount ();
-
- if (now >= gFlyTime) {
- gWhichFly = gWhichFly ^ 1; // toggle between 0 and 1
- MySetCursor (&GetINITData () -> flyCursors [gWhichFly]);
- gFlyTime = now + kFlyAnimateDelay;
- }
- }
-
- static void PrepareFlyCursors (Side whichSide)
- {
- // Move the hotspot to ensure a visible fly
-
- FlyPaperGestaltPtr flyPaperINITData = GetINITData ();
-
- if (whichSide == kLeftSide) {
- flyPaperINITData -> flyCursors [0].hotSpot.h = 0;
- flyPaperINITData -> flyCursors [1].hotSpot.h = 0;
- } else {
- flyPaperINITData -> flyCursors [0].hotSpot.h = 15;
- flyPaperINITData -> flyCursors [1].hotSpot.h = 15;
- }
- }
-
- static Boolean IsMousePegged (Point mouse, Side* whichSide)
- {
- Boolean pegged;
-
- if (gNarrowGrayRgn == NULL) {
- THz oldZone = GetZone ();
- SetZone (SystemZone ());
- gNarrowGrayRgn = NewRgn ();
- CopyRgn (GetGrayRgn (), gNarrowGrayRgn);
- SetZone (oldZone);
- InsetRgn (gNarrowGrayRgn, 2, 0);
- gGrayRgnRect = (**GetGrayRgn()).rgnBBox;
- }
-
- /* return true if mouse is pegged to edges */
- pegged = PtInRgn (mouse, GetGrayRgn ()) && !PtInRgn (mouse, gNarrowGrayRgn);
- if (pegged) {
- if (mouse.h - gGrayRgnRect.left > gGrayRgnRect.right - mouse.h)
- *whichSide = kRightSide;
- else
- *whichSide = kLeftSide;
- return true;
- } else
- return false;
- }
-
- static
- pascal OSErr myInputProc (Point* mouse, short *modifiers, void *dragInputRefCon,
- DragReference theDragRef)
- {
- EnterCodeResource ();
- OSErr error = noErr;
- Side whichSide;
-
- if (gMachineState == kStartedPegged) {
- if ((*modifiers & btnState) == 0 && !IsMousePegged (*mouse, &whichSide)) {
- gMachineState = kDraggingState;
- }
- } else if (gMachineState == kDraggingState) {
- if ((*modifiers & btnState) == 0 && IsMousePegged (*mouse, &whichSide) &&
- (CheckAcceptability (theDragRef) == kAcceptable)) {
- gMachineState = kPeggedState;
- PrepareFlyCursors (whichSide);
- RememberCursor ();
- AnimateFly ();
- }
- } else if (gMachineState == kPeggedState) {
- if ((*modifiers & btnState) == 0) {
- if (IsMousePegged (*mouse, &whichSide)) {
- AnimateFly ();
- } else {
- gMachineState = kDraggingState;
- MySetCursor (&gOldCursor);
- }
- } else {
- if (IsMousePegged (*mouse, &whichSide)) {
- gMachineState = kDroppedState;
- MySetCursor (&gOldCursor);
-
- WindowPtr w = GetINITData () -> magicWindow;
- if (w) {
- MoveWindow (w, mouse -> h - 1, mouse -> v - 1, false);
- }
- }
- }
- }
-
- ExitCodeResource ();
- return error;
- }
-
- static
- void myNewDrag (NewDragParms* parms)
- {
- gMachineState = kIdleState;
-
- // Force recalculation of gray rgn
-
- if (gNarrowGrayRgn) {
- DisposeRgn (gNarrowGrayRgn);
- gNarrowGrayRgn = NULL;
- }
- }
-
- static
- void mySetDragInputProc (SetDragInputProcParms* parms)
- {
- }
-
- static
- void myTrackDrag (TrackDragParms* parms)
- {
- OSErr error;
-
- error = SetDragInputProc (parms -> theDragRef, myInputProc, 0);
- if (error) {
- gMachineState = kIdleState;
- } else {
-
- FlyPaperGestaltPtr data = GetINITData ();
- if (data && data -> magicWindow) {
- Side whichSide;
- if (IsMousePegged (parms -> theEvent -> where, &whichSide))
- gMachineState = kStartedPegged;
- else
- gMachineState = kDraggingState;
- gAcceptability = kUnknown;
- } else
- gMachineState = kIdleState;
- }
- }
-
- void main (short selector, void* stackFrame)
- {
- EnterCodeResource ();
-
- switch (selector) {
- case kNewDragSelector : myNewDrag ((NewDragParms*) stackFrame); break;
- case kSetDragInputProcSelector : mySetDragInputProc ((SetDragInputProcParms*) stackFrame); break;
- case kTrackDragSelector : myTrackDrag ((TrackDragParms*) stackFrame); break;
- }
-
- ExitCodeResource ();
- }
-